<template>
  <div class="content">
    <!-- 被祝福区域 -->
    <div class="blessed" v-if="selectType === '1' || selectType === '2'">
      <span>{{selectType === '1'?'被祝福人':'被告白人'}}</span>
      <input v-model="dataInfo.by_name" placeholder="建议填写真实姓名哦（10字内）" />
    </div>
    <!-- 自定义区域 -->
    <div class="blessed" v-if="selectType === '5'">
      <span>自定义发布类型:</span>
      <input v-model="dataInfo.def_type" placeholder="是失物招领?还是想对我说你有女朋友了" />
    </div>
    <!-- 内容填写区域 -->
    <h3>{{types}}内容填写:</h3>
    <div class="msg">
      <h5 v-show="selectType === '4'">分享欲可是最高级的浪漫哦!</h5>
      <textarea rows="3" cols="60" v-model="dataInfo.message" :placeholder="placeholder"></textarea>
      <div class="num">
        <span>{{message}}</span>&nbsp;/100
      </div>
    </div>
    <!-- 通知区域 -->
    <div class="inform">
      <el-switch
        v-model="is_infrom"
        active-color="#008c8c"
        inactive-color="#ccc"
        @change="changeSwitch"
      ></el-switch>
      <a class="hint" title="快让他知道吧">@他(她) &emsp;</a>
      <div class="inform-item" v-if="is_infrom">
        <div>
          <span>昵称查询:&ensp;</span>
          <input type="text" ref="inputInform" v-model="informuser" placeholder="必须查询到用户墙墙才会去通知哦" />
          <button class="btn" @click="selectuser">查询</button>
        </div>
        <!-- 查询结果展示区域 -->
        <div class="containers" v-if="Object.keys(informuserData).length !== 0 && informResult">
          <div class="headerimg">
            <!-- 有头像则显示 -->
            <img :src="informuserData.user_pic" v-if="informuserData.user_pic" />
            <div class="anonymity" v-else>匿名</div>
          </div>
          <div class="contents">
            <!-- 详细个人消息 -->
            <div class="userinfo">
              <h3>{{informuserData.nickname?informuserData.nickname:'匿名' }}</h3>
              <p class="timer">简介 {{informuserData.intro?informuserData.intro:'?'}}</p>
              <p class="timer">
                来自
                <a>{{informuserData.from?informuserData.from:'?'}}</a>
              </p>
            </div>
          </div>
        </div>
        <div
          v-if="Object.keys(informuserData).length === 0 && !informResult"
        >抱歉！ 根据 {{informuser}} 关键词没有查询到用户</div>
      </div>
    </div>
    <!-- 上传图片区域 -->
    <div class="update">
      <h3>
        分享图片
        <a>(注意:最多只能上传4张图片且大小不能高于2MB)</a>
      </h3>
      <el-upload
        action="http://127.0.0.1:8889/dev/updateAvatar"
        list-type="picture-card"
        :on-preview="handlePictureCardPreview"
        :before-upload="beforeAvatarUpload"
        :on-remove="handleRemove"
        :on-change="changeUpload"
        :auto-upload="false"
        :limit="4"
        :on-exceed="onExceed"
      >
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt />
      </el-dialog>
    </div>
    <!-- 保存区域 -->
    <div class="save">
      <button @click="save">保存发布</button>
      <button>延迟发布</button>
    </div>
  </div>
</template>

<script>
export default {
  props: ["selectType"],
  data() {
    return {
      types: "心愿",
      placeholder: "你的心愿内容:限制100字",
      is_infrom: false, //是否通知，默认是false
      dialogImageUrl: "", //放大的图片elment_ul
      dialogVisible: false, //是否显示放大图片
      informuser: "", //绑定通知用户
      informuserData: {}, //查询通知用户结果
      informResult: true, //查询结果为空
      // 收集数据
      dataInfo: {
        message: "",
        isName: true,
        by_name: undefined, //被祝福的和告白人名字，关键字
        def_type: undefined, //自定义类型 用于其他墙
        //还有一个图片字段pic_walls
      },
      formdata: [], //图片存放的数据数组
    };
  },
  methods: {
    // 保存发送数据
    save() {
      let { dataInfo, formdata } = this;
      // 判断发布内容是否为空 到时候去后端解决空格问题
      if (!dataInfo.message) {
        return this.$message("发布内容不能为空");
      }
      // num_types数字类型，cha_types文字类型
      dataInfo.num_types = this.selectType;
      dataInfo.cha_types = this.types;
      // 判断用户是否查询需要通知 有值则使用该id，没有则为false
      if (Object.keys(this.informuserData).length === 0 && !this.is_infrom) {
        dataInfo.inform = false;
      } else {
        dataInfo.inform = this.informuserData.uuId;
      }
      // 因为转码需要时间，我们放个异步，避免未完成转码就发送请求
      this.$emit("getDataInfo", dataInfo, formdata);
      // 将数据清空
      dataInfo = { isName: true };
    },
    // 删除上传图片回调
    handleRemove(file, fileList) {
      this.dataInfo.pic_walls = fileList;
    },
    // 查看放大图片回调
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 文件状态改变时的钩子，添加文件、上传成功和上传失败时都会被调用
    changeUpload(file, fileList) {
      this.formdata = fileList;
    },
    // 上传前的回调
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isLt2M;
    },
    // 图片上传超额回调
    onExceed() {
      this.$message({
        type: "error",
        message: "最多分享4张图片哦",
      });
    },
    // 点击查看的回调
    async selectuser() {
      if (!this.informuser.trim()) return this.$message("查询值不能为空");
      let result = await this.$API.getinformuser(this.informuser);
      console.log(result);
      if (result.code === 200 && Object.keys(result.data).length !== 0) {
        this.informuserData = result.data;
      } else {
        this.informuserData = {}; //清空
        this.informResult = false;
      }
    },
    // switch开关切换的回调
    changeSwitch(e) {
      if (!e) {
        // 清空数据
        (this.informuser = ""),
          (this.informuserData = {}),
          (this.informResult = true);
      } else {
        // 当模块加载完成时再去获取焦点
        this.$nextTick(() => {
          this.$refs.inputInform.focus();
        });
      }
    },
  },
  computed: {
    // 截取长度
    message() {
      if (!this.dataInfo.message) return 0; //如果没有值就直接返回
      if (this.dataInfo.message.length >= 100) {
        // 截取前20位
        this.dataInfo.message = this.dataInfo.message.slice(0, 100);
      }
      return this.dataInfo.message.length;
    },
  },
  watch: {
    selectType() {
      Object.assign(this.$data, this.$options.data(this)); //重置data里的值
      // 先清除数据 再切换
      let { selectType } = this;
      if (selectType === "0") {
        this.types = "心愿";
        this.placeholder = "你的心愿内容:限制100字";
      } else if (selectType === "1") {
        this.types = "祝福";
        this.placeholder = "你的祝福内容:限制100字";
      } else if (selectType === "2") {
        this.types = "表白";
        this.placeholder = "你的表白内容:限制100字";
      } else if (selectType === "3") {
        this.types = "一起玩";
        this.placeholder = "你的邀请内容:限制100字";
      } else if (selectType === "4") {
        this.types = "每日分享";
        this.placeholder = "你的分享内容:限制100字";
      } else if (selectType === "5") {
        this.types = "其他";
        this.placeholder = "是失物招领?还是想对墙分享你有女朋友了";
      }
    },
  },
};
</script>

<style lang="less" scoped>
.content {
  padding: 20px 0;
  h3 {
    font-weight: 700;
    color: #008c8c;
  }
  h5 {
    font-size: 12px;
    color: #a2b6aa;
  }
  .blessed {
    padding: 10px 0;
    span {
      font-weight: 700;
      color: #008c8c;
    }
    input {
      width: 50%;
      margin-left: 5px;
      text-indent: 5px;
      outline: none;
      border: none;
      border-bottom: 1px solid #008c8c;
    }
  }
  .msg {
    width: auto;
    padding: 10px 0;
    textarea {
      border-radius: 5px;
      text-indent: 5px;
    }
    .num {
      text-align: center;
      span {
        color: #008c8c;
      }
    }
  }
  .inform {
    padding: 10px 0;
    a {
      color: #121212;
    }
    .inform-item {
      padding: 10px 0;
      cursor: pointer;
      span {
        font-weight: 700;
        color: #008c8c;
        a {
          font-size: 12px;
          color: #008c8c;
        }
      }
      input {
        width: 300px;
        padding: 0 5px;
        outline: none;
        border: none;
        border-bottom: 1px solid #008c8c;
      }
      .btn {
        margin-left: 20px;
        padding: 5px 15px;
        border: none;
        background: #008c8c;
        color: #fff;
        text-align: center;
        border-radius: 5px;
        cursor: pointer;
      }
    }
  }
  .hint {
    margin-left: 10px;
  }
  .update {
    h3 {
      margin-bottom: 10px;
      a {
        font-weight: normal;
        font-size: 12px;
        color: #8590a6;
      }
    }
  }
  .save {
    padding: 10px 0;
    text-align: right;
    button {
      border: none;
      outline: none;
      border-radius: 5px;
      padding: 5px 10px;
      background: #008c8c;
      color: #fff;
      margin: 0 20px;
    }
  }
  .containers {
    display: flex;
    justify-content: start;
    padding: 10px 0;
    min-width: 718px;
    border-bottom: 1px solid #008c8c;
    .headerimg {
      width: 50px;
      height: 50px;
      padding-right: 10px;
      img {
        margin-top: 5px;
        border-radius: 50%;
        width: 100%;
        height: 100%;
        cursor: pointer;
      }
      .anonymity {
        width: 50px;
        height: 50px;
        line-height: 50px;
        text-align: center;
        border-radius: 50%;
        background: #8590a6;
        color: #fff;
        font-size: 18px;
        font-weight: 700;
        margin-right: 10px;
      }
    }
    .contents {
      width: 578px;
      .userinfo {
        h3 {
          font-size: 16px;
          font-weight: 700;
          cursor: pointer;
        }
        h3:hover {
          color: #008c8c;
        }
        .timer {
          font-size: 12px;
          color: #008c8c;
          text-overflow: ellipsis;
          overflow: hidden;
          white-space: nowrap;
        }
      }
    }
    .visit {
      width: 80px;
      height: 60px;
      line-height: 60px;
      font-size: 12px;
      color: #008c8c;
      span {
        cursor: pointer;
      }
    }
  }
}
</style>